local PartnerRelationRender = app.mvc.createRenderClass(...)

local PartnerHeadIconRender = import(".PartnerHeadIconRender")

local NewPartnerRelationConfig = {
    {line = "partner_bar_h.png", point1 = "partner_point_h.png", point2 = "partner_point_h_l.png"},

    {line = "partner_bar_lv.png", point1 = "partner_point_lv.png", point2 = "partner_point_lv_l.png"},

    {line = "partner_bar_l.png", point1 = "partner_point_l.png", point2 = "partner_point_l_l.png"},
    {line = "partner_bar_l.png", point1 = "partner_point_l.png", point2 = "partner_point_l_l.png"},

    {line = "partner_bar_z.png", point1 = "partner_point_z.png", point2 = "partner_point_z_l.png"},
    {line = "partner_bar_z.png", point1 = "partner_point_z.png", point2 = "partner_point_z_l.png"},
    {line = "partner_bar_z.png", point1 = "partner_point_z.png", point2 = "partner_point_z_l.png"},

    {line = "partner_bar_c.png", point1 = "partner_point_c.png", point2 = "partner_point_c_l.png"},
    {line = "partner_bar_c.png", point1 = "partner_point_c.png", point2 = "partner_point_c_l.png"},
    {line = "partner_bar_c.png", point1 = "partner_point_c.png", point2 = "partner_point_c_l.png"},
    {line = "partner_bar_c.png", point1 = "partner_point_c.png", point2 = "partner_point_c_l.png"},
}

--data =
function PartnerRelationRender.create(node)
    local sprite = PartnerRelationRender.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end

function PartnerRelationRender:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    

    self._iconRenders = {}
    for i =1, 8 do
        self._iconRenders[i] = PartnerHeadIconRender.create(self._ui["Panel/icon_"..i])
    end

    self._ui["Panel/Panel_light"]:setTouchEnabled(false)
    for i = 1, 10 do
        local point = self._ui["Panel/Panel_bg/"..i]
        point:setTouchEnabled(true)
        point:addClickEventListener(function(sender)
            if self._data then
                local relations = self._data.relations
                local params = {}
                params.tipsType = Enums.TipsType.contents
                params.contents = self:_getTipContents(relations[i])
                local otherParams = {}
                local size = sender:getContentSize()
                local pos = sender:convertToWorldSpace(cc.p(0,0))
                otherParams.tipPos = cc.p(pos.x - size.width/2, pos.y + size.height)
                EM:notify(ViewEvent.Partner_ShowTips, params, otherParams)
            end
        end)
    end
    self:refresh()

end

function PartnerRelationRender:_getTipContents(relationData)
    local contents = {}
    local part1 = {}
    local q = relationData:getQuality()

    local colorNormal = cc.c3b(255, 225, 132)
    local colorGreen = cc.c3b(0, 220, 220)
    local colorGray = cc.c3b(160, 160, 160)
    table.insert(part1, {text = L("PartnerRelationTipTitle"), color = colorNormal})
    if q == 0 then
        table.insert(part1, {text = L("lua_code_text_124"), color = colorGray})
    else
        table.insert(part1, {text = string.format(L("PartnerRelationTipQuality"), relationData:getColorString(q)), color = relationData:getColor(q)})
    end

--[[
    local props = relationData:getShowProps()
    for k, v in pairs(props) do
        local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
        table.insert(part1, {text = string.format(L("%s%s"), name or "", value or ""), color = colorGreen})
    end
    --]]
    table.insert(part1,{text = string.format(L("PartnerRelationScore"), relationData:getScore()), color = colorGreen} )

    table.insert(contents, part1)
    local part2 = {}

    --[[
    local nextProps = relationData:getShowProps(q+1)
    if #nextProps > 0 then
        table.insert(part2, {text = L("ui_csd_271"), color = colorNormal})
        for k, v in pairs(nextProps) do
            local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
            table.insert(part2, {text = string.format(L("%s%s"), name or "", value or ""), color = colorGray})
        end
    end
    --]]

    local nextScore = relationData:getScore(q+1)
    if nextScore then
        table.insert(part2, {text = L("ui_csd_271"), color = colorNormal})
        table.insert(part2,{text = string.format(L("PartnerRelationScore"), nextScore), color = colorGray} )
    end


    local upTip = relationData:getUpgradeTip()
    if upTip ~= "" then
        table.insert(part2, {text = upTip, color = colorNormal})
    end
    if #part2 > 0 then
        table.insert(contents, part2)
    end    
    return contents
end

function PartnerRelationRender:refresh()
    if not self._data then
        return
    end
    local partners = self._data.partners
    for i =1, 8 do
        self._iconRenders[i]:setData(partners[i])
    end

    local relations = self._data.relations
    for i = 1, 10 do
        self:_updateRelation(i, relations[i])
    end
end


function PartnerRelationRender:_createAction()
    local action = cc.RotateBy:create(2, 360)
    
    return cc.RepeatForever:create(action)
end

function PartnerRelationRender:_updateRelation(index, rData)
    if not rData then
        return
    end
    local curQ, nextQ, q1, q2 = rData:getQuality()
    local line1 = self._ui["Panel/Panel_light/"..index.."a"]
    local line2 = self._ui["Panel/Panel_light/"..index.."b"]
    local point = self._ui["Panel/Panel_light/"..index]

    line1:setVisible(q1 ~= 0)
    line2:setVisible(q2 ~= 0)
    point:setVisible(nextQ ~= 0)

    local lineConf1 = NewPartnerRelationConfig[q1]
    local lineConf2 = NewPartnerRelationConfig[q2]
    local curConf = NewPartnerRelationConfig[curQ]
    if lineConf1 then
        line1:loadTexture(Res.PartnerRelationPath .. lineConf1.line)
    end

    if lineConf2 then
        line2:loadTexture(Res.PartnerRelationPath .. lineConf2.line)
    end

    point:stopAllActions()
    if curConf then
        if curQ ~= 0 then
            point:loadTexture(Res.PartnerRelationPath .. curConf.point2)
            point:runAction(self:_createAction())
        else
            point:loadTexture(Res.PartnerRelationPath .. curConf.point1)
        end
    end
end

return PartnerRelationRender